// $codepro.audit.disable
/**
 * License under GNU's GPL 
 * Gui.java 
 */
/**
 * License under GNU's GPL 
 * Admin.java 
 */

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


/**
 * Class holds the first GUI with login details for the system
 * 
 * @author Team 9
 * @version 1.0
 * 
 */
public class Gui {
	/**
	 * All users
	 */
	private static List<User> users;
	
	/** 
	 * Getter for the user array
	 * @return the user array
	 */
	public static List<User> getUsers() {
		return users;
    }	
	
	/**
	 * Setter for the users array
	 * @param users the new user array
	 */

	public static void setUsers(List<User> users) {
		Gui.users = users;
	}
	
	/**
	 * gets a list of users from the text files
	 * @throws FileNotFoundException
	 */
	// compiles list from text files
	public void getUser() throws FileNotFoundException {
		final String[] types = { "Patient", "Nurse", "Doctor", "Admin" };
		for (int i = 0; i < 4; i++) {
			Scanner fileScan = new Scanner(new File(types[i] + ".txt"));
			fileScan.useDelimiter(" ");
			while (fileScan.hasNext()) {
				String first = fileScan.next();
				String last = fileScan.next();
				String usname = fileScan.next();
				String pass = fileScan.next();
				fileScan.nextLine();
				if (i == 0) {
					patientArray.add(new Patient(first, last, usname, pass));
				} else if (i == 1) {
					nurseArray.add(new Nurse(first, last, usname, pass));
				} else if (i == 2) {
					doctorArray.add(new Doctor(first, last, usname, pass));
				} else if (i == 3) {
					adminArray.add(new Admin(first, last, usname, pass));
				}
			}
		}
	}

	/**
	 * constant value 100
	 */
	private static final int HUNDRED = 100;

	/**
	 * constant value 450
	 */
	private static final int FOURFIFTY = 450;

	/**
	 * constant value 300
	 */
	private static final int THREEHUNDRED = 300;

	/**
	 * constant value 75
	 */
	private static final int SEVENTYFIVE = 75;

	/**
	 * constant value 5
	 */
	private static final int FIVE = 5;

	/**
	 * constant value 2
	 */
	private static final int TWO = 2;

	/**
	 * constant value 10
	 */
	private static final int TEN = 10;

	/**
	 * constant value 3
	 */
	private static final int THREE = 3;

	/**
	 * constant value 4
	 */
	private static final int FOUR = 4;

	/**
	 * constant value 6
	 */
	private static final int SIX = 6;

	/**
	 * Prints string
	 * @return string with data
	 */
	@Override
	public String toString() {
		return "Gui [count=" + count + "]";
	}

	/**
	 * JFrame to hold the main window for the GUI
	 */
	private JFrame frame;

	/**
	 * JTextfield for taking user input for username
	 */
	private JTextField textField;

	/**
	 * JPassword for taking user input for password
	 */
	private JPasswordField passwordField;

	/**
	 * JLabel for putting various labels on the GUI
	 */
	private JLabel label;

	/**
	 * JButton, to be pressed after entering details
	 */
	private JButton btnLogin;

	/**
	 * LoginListener to check for login
	 */
	private LoginListener login;

	/**
	 * int variable to count number of login failures
	 */
	private int count = 0;

	/**
	 * Arraylist to hold all the nurses
	 */
	private static List<Nurse> nurseArray = new ArrayList<Nurse>();

	/**
	 * Arraylist to hold all the doctors
	 */
	private static List<Doctor> doctorArray = new ArrayList<Doctor>();
	
	/**
	 * Arraylist to hold patients
	 */
	private static List<Patient> patientArray = new ArrayList<Patient>();
	
	/**
	 * Arraylist to hold admins
	 */
	private static final List<Admin> adminArray = new ArrayList<Admin>();

	/**
	 * Arraylist to hold appointments
	 */
	private static List<Appointment> appointArray = new ArrayList<Appointment>();
	
	/**
	 * Getter for the appointment array
	 * @return the appointment array
	 */
	public static List<Appointment> getAppointArray() {
		return appointArray;
	}
	
	/**
	 * Setter for the appointment array
	 * @param appointArray the new appointment array
	 */
	public static void setAppointArray(List<Appointment> appointArray) {
		Gui.appointArray = appointArray;
	}
	
	/**
	 * Adds a new patient to the patient array
	 * @param pat the new patient being added
	 */
	public static void addToPatientArray(Patient pat) {
		patientArray.add(pat);
	}
	
	/**
	 * Adds a new nurse to the nurse array
	 * @param nurse the new nurse being added
	 */
	public static void addToNurseArray(Nurse nurse) {
		nurseArray.add(nurse);
	}
	
	/**
	 * Adds a new doctor to the doctor array
	 * @param doc the new doctor being added
	 */
	public static void addToDoctorArray(Doctor doc) {
		doctorArray.add(doc);
	}
	
	/**
	 * Getter for the nurse Array
	 * @return the nurse array
	 */
	public static List<Nurse> getNurseArray() {
		return nurseArray;
	}
	
	/**
	 * Setter for the nurse Array
	 * @param nurseArray the new nurse array
	 */
	public static void setNurseArray(List<Nurse> nurseArray) {
		Gui.nurseArray = nurseArray;
	}
	
	/**
	 * Getter for the doctor Array
	 * @return the doctor array
	 */
	public static List<Doctor> getDoctorArray() {
		return doctorArray;
	}
	
	/**
	 * Setter for the doctor Array
	 * @param doctorArray the new doctor array
	 */
	public static void setDoctorArray(List<Doctor> doctorArray) {
		Gui.doctorArray = doctorArray;
	}
	
	/**
	 * Getter for the patient Array
	 * @return the patient array
	 */

	public static List<Patient> getPatientArray() {
		return patientArray;
	}
	
	/**
	 * Setter for the patient Array
	 * @param patientArray the new patient array
	 */

	public static void setPatientArray(List<Patient> patientArray) {
		Gui.patientArray = patientArray;
	}

	/**
	 * Runs the application
	 * @param args -
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					final Gui window = new Gui();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Gui() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(HUNDRED, HUNDRED, FOURFIFTY, THREEHUNDRED);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 120, 201, 120, 0 };
		gridBagLayout.rowHeights = new int[] { SEVENTYFIVE, 0, 0, 0, 0, 0,
				SEVENTYFIVE, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, Double.MIN_VALUE };
		frame.getContentPane().setLayout(gridBagLayout);

		final JLabel lblLogin = new JLabel("User Name");
		final GridBagConstraints gbc_lblLogin = new GridBagConstraints();
		gbc_lblLogin.insets = new Insets(0, 0, FIVE, FIVE);
		gbc_lblLogin.gridx = 1;
		gbc_lblLogin.gridy = 1;
		frame.getContentPane().add(lblLogin, gbc_lblLogin);

		textField = new JTextField();
		final GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.insets = new Insets(0, 0, FIVE, FIVE);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = TWO;
		frame.getContentPane().add(textField, gbc_textField);
		textField.setColumns(TEN);

		final JLabel lblPassword = new JLabel("Password");
		final GridBagConstraints gbc_lblPassword = new GridBagConstraints();
		gbc_lblPassword.insets = new Insets(0, 0, FIVE, FIVE);
		gbc_lblPassword.gridx = 1;
		gbc_lblPassword.gridy = THREE;
		frame.getContentPane().add(lblPassword, gbc_lblPassword);

		passwordField = new JPasswordField();
		final GridBagConstraints gbc_passwordField = new GridBagConstraints();
		gbc_passwordField.insets = new Insets(0, 0, FIVE, FIVE);
		gbc_passwordField.fill = GridBagConstraints.HORIZONTAL;
		gbc_passwordField.gridx = 1;
		gbc_passwordField.gridy = FOUR;
		frame.getContentPane().add(passwordField, gbc_passwordField);

		label = new JLabel("Please log in here.");
		final GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.insets = new Insets(0, 0, 0, FIVE);
		gbc_label.gridx = 1;
		gbc_label.gridy = SIX;
		frame.getContentPane().add(label, gbc_label);

		btnLogin = new JButton("Login");
		login = new LoginListener();
		btnLogin.addActionListener(login);
		final GridBagConstraints gbc_btnLogin = new GridBagConstraints();
		gbc_btnLogin.insets = new Insets(0, 0, FIVE, FIVE);
		gbc_btnLogin.gridx = 1;
		gbc_btnLogin.gridy = FIVE;
		frame.getContentPane().add(btnLogin, gbc_btnLogin);

	}
	
	/**
	 * Getter for the JFrame
	 * @return the jframe the GUI is in
	 */
	public JFrame getFrame() {
		return frame;
	}
	
	/**
	 * Setter for the JFrame
	 * @param frame the new frame
	 */
	public void setFrame(JFrame frame) {
		this.frame = frame;
	}

	/**
	 * LoginListner for the login GUI page
	 * 
	 * @author Team 9
	 * 
	 */
	private class LoginListener implements ActionListener {
		/**
		 * Methods prints string
		 * @return returns the needed data
		 */
		@Override
		public String toString() {
			return "LoginListener []";
		}

		@Override
		/**
		 * Holds action listner for event
		 */
		public void actionPerformed(ActionEvent arg0) {
			final Admin bacon = new Admin("Admin", "Amdin", "admin", "bacon");
			try {
				getUser();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			Scanner fileScan;
			try {
				fileScan = new Scanner(new File("Appointments.txt"));
				fileScan.useDelimiter(" ");
				while (fileScan.hasNext()) {
					String date = fileScan.next();
					String mm = date.substring(0, 2);
					String dd = date.substring(3, 5);
					String yy = date.substring(6);

					String time = fileScan.next();
					String hh = time.substring(0, 2);
					String min = time.substring(3, 5);
					String ap = time.substring(5);

					String doc = fileScan.next();
					fileScan.nextLine();

					appointArray.add(new Appointment(mm, dd, yy, hh, min, ap,
							doc));
				}

			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			users = new ArrayList<User>();
			for (int i = 0; i < patientArray.size(); i++) {
				patientArray.get(i).setType(3);
				users.add(patientArray.get(i));
			}
			for (int i = 0; i < nurseArray.size(); i++) {
				nurseArray.get(i).setType(2);
				users.add(nurseArray.get(i));
			}
			for (int i = 0; i < doctorArray.size(); i++) {
				doctorArray.get(i).setType(1);
				users.add(doctorArray.get(i));
			}
			for (int i = 0; i < adminArray.size(); i++) {
				adminArray.get(i).setType(0);
				users.add(adminArray.get(i));
			}

			users.add(bacon);

			count++;

			final String userName = textField.getText().trim();
			final char[] passWordchars = passwordField.getPassword();
			final String passWord = new String(passWordchars);

			boolean un = false;
			boolean pw = false;

			User temp = null;

			for (int i = 0; i < users.size(); i++) {
				if (users.get(i).getUserName().equals(userName)) {
					un = true;
					temp = users.get(i);
					break;
				}
			}
			if (temp != null) {
				if (temp.getPassword().equals(passWord)) {
					pw = true;
				}
			}

			if (un && pw) {
				int temp2 = temp.getType();
				label.setText("Logged in: " + temp.getFirstName() + " "
						+ temp.getLastName() + " " + temp2);
				if (temp2 == 3) {
					temp.showGUI();
				} else if (temp2 == 0) {
					temp.showGUI();
				} else if (temp2 == 1) {
					temp.showGUI();
				} else if (temp2 == 2) {
					temp.showGUI();
				}
			} else {
				if (count != THREE) {
					label.setText("Not logged in.");
				}
				if (count == THREE) {
					btnLogin.setText("Locked out.");
				}
			}
			if (btnLogin.getText().equals("Locked out.")) {
				btnLogin.removeActionListener(login);
			}
		}
	}
}
